package czy.demo.controller;


import czy.demo.pojo.Person;
import czy.demo.service.PersonService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "缓存测试")
@RestController
@RequestMapping("/person")
public class PersonController {

    private static Logger logger = LoggerFactory.getLogger(PersonController.class);

    @Autowired
    private PersonService personService;

    @GetMapping("/get/all")
    public Map<Integer,Person> getAll(){
        return this.personService.getAll();
    }

    /**
     * Cacheable注解不仅可以加到组件方法中，也可以加在Controller方法上
     * 由于Controller中总是用于处理参数，所以不建议放在这里
     */
    @GetMapping("/get/{id}")
    public Person get(@PathVariable Integer id){
        logger.info("调用controller中get方法一次");
        return this.personService.get(id);
    }

    @PostMapping("/create")
    public String create(@RequestBody Person person){
        logger.info("调用controller中create方法一次");
        this.personService.create(person);
        return "创建成功";
    }

    @PutMapping("/update")
    public String update(@RequestBody Person person){
        logger.info("调用controller中update方法一次");
        this.personService.update(person);
        return "更新成功";
    }

    @DeleteMapping("/delete/{id}")
    public String delete(@PathVariable Integer id){
        logger.info("调用controller中delete方法一次");
        this.personService.delete(id);
        return "删除成功";
    }

}
